﻿using BaoVeBienCuong.Playgame.enemy;
using BaoVeBienCuong.SelectMap;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using System.Collections.Generic;

namespace BaoVeBienCuong.Playgame.Button
{
    class Fire
    {
        private int Type;//0 là mưa lửa, 1 là bẫy lửa
        private Vector2 position;//vị trí đặt
        private Vector2 size;//kích thước
        private Texture2D image;//ảnh
        private float Dame;//sát thương
        private bool isEnable;//hiển thị
        private bool isBum;//nổ bùm bùm
        private float scale;//tỉ lệ
        private Vector2 index;//vị trí trong map
        private int count;
        public static int SoLuong = 0;
        private string HieuUng;// mưa lửa có hiệu ứng thiêu đốt, bẫy lửa có hiệu ứng trói
        private int soluong;
        public Fire()
        {
            count = 0;
            isEnable = false;
            isBum = false;
            scale = 0.5f;
            Dame = 100;
        }
        public Fire(int type, SaveKyNang save)
        {
            count = 0;
            isEnable = false;
            isBum = false;
            scale = 0.5f;
            setType(type);
            soluong = save.getFire(Type, ref Dame);
        }
        public void setIndex(Vector2 index)
        {
            this.index = index;
            size = new Vector2(GamePage.sizeImageBackGround.X / GamePage.sizeMap.Y, GamePage.sizeImageBackGround.Y / GamePage.sizeMap.X);
            position = new Vector2(index.Y * size.X, index.X * size.Y);
            isEnable = true;
            isBum = false;
            SoLuong++;
        }
        public void Load(ContentManager content)
        {
            if (Type == 0)
            {
                image = content.Load<Texture2D>("images/magic/rain_of_fire");
            }
            else
            {
                if (isBum)
                {
                    image = content.Load<Texture2D>("images/magic/trap_effect");
                }
                else
                {
                    image = content.Load<Texture2D>("images/magic/trap");
                }
            }
        }
        public void Draw(SpriteBatch sprite)
        {
            scale = GamePage.scale;
            if (Type == 0)
            {
                #region
                if (count < 35)
                {
                    sprite.Draw(image, (position + new Vector2(-12, -200)) * scale + GamePage.DoLech, new Rectangle(105 * (count / 5), 0, 105, 226), Color.White, 0, Vector2.Zero, scale * 4 / 3, SpriteEffects.None, 0);
                }
                if (count >= 10 && count < 45)
                {
                    sprite.Draw(image, (position + new Vector2(-2, -180)) * scale + GamePage.DoLech, new Rectangle(105 * ((count - 10) / 5), 0, 105, 226), Color.White, 0, Vector2.Zero, scale * 4 / 3, SpriteEffects.None, 0);
                }
                if (count >= 20 && count < 55)
                {
                    sprite.Draw(image, (position + new Vector2(-22, -210)) * scale + GamePage.DoLech, new Rectangle(105 * ((count - 20) / 5), 0, 105, 226), Color.White, 0, Vector2.Zero, scale * 4 / 3, SpriteEffects.None, 0);
                }
                if (count >= 30 && count < 65)
                {
                    sprite.Draw(image, (position + new Vector2(-12, -180)) * scale + GamePage.DoLech, new Rectangle(105 * ((count - 30) / 5), 0, 105, 226), Color.White, 0, Vector2.Zero, scale * 4 / 3, SpriteEffects.None, 0);
                }
                if (count >= 40 && count < 75)
                {
                    sprite.Draw(image, (position + new Vector2(-12, -200)) * scale + GamePage.DoLech, new Rectangle(105 * (count / 5), 0, 105, 226), Color.White, 0, Vector2.Zero, scale * 4 / 3, SpriteEffects.None, 0);
                }
                if (count >= 50 && count < 85)
                {
                    sprite.Draw(image, (position + new Vector2(-2, -180)) * scale + GamePage.DoLech, new Rectangle(105 * ((count - 10) / 5), 0, 105, 226), Color.White, 0, Vector2.Zero, scale * 4 / 3, SpriteEffects.None, 0);
                }
                if (count >= 60 && count < 95)
                {
                    sprite.Draw(image, (position + new Vector2(-22, -210)) * scale + GamePage.DoLech, new Rectangle(105 * ((count - 10) / 5), 0, 105, 226), Color.White, 0, Vector2.Zero, scale * 4 / 3, SpriteEffects.None, 0);
                }
                if (count == 0 || count == 35 || count == 70) MainPage.PlayHieuUng("RainOfFire", false);
                if (!GamePage.isPause) count++;
                if (count >= 95)
                {
                    isEnable = false;
                    GamePage.curmap[(int)index.X, (int)index.Y] = 1;
                    SoLuong--;
                    count = 0;
                }
                #endregion
            }
            else
            {
                if (isBum)
                {
                    sprite.Draw(image, (position + new Vector2(-30, -60)) * scale + GamePage.DoLech, new Rectangle(140 * (count / 4), 0, 140, 131), Color.White, 0, Vector2.Zero, scale * 4 / 3, SpriteEffects.None, 0);
                    if (!GamePage.isPause) count++;
                    if (count == 24)
                    {
                        isEnable = false;
                        GamePage.curmap[(int)index.X, (int)index.Y] = 1;
                        SoLuong--;
                        count = 0;
                    }
                }
                else
                {
                    sprite.Draw(image, (position + new Vector2(-10, -10)) * scale + GamePage.DoLech, new Rectangle(229 * (count / 5), 0, 229, 192), Color.White, 0, Vector2.Zero, scale * 2 / 3, SpriteEffects.None, 0);
                    if (!GamePage.isPause) count++;
                    if (count == 25)
                    {
                        count = 0;
                    }
                }
            }
        }
        public void setType(int type)
        {
            Type = type;
            if (Type == 0)
            {
                HieuUng = "chay mau";
            }
            else
            {
                HieuUng = "lam do";
            }
        }
        public bool getEnable()
        {
            return isEnable;
        }
        public void SatThuong(List<Enemy> enemy)
        {
            if ((Type == 1 && isBum == false) || Type == 0)
            {
                for (int j = 0; j < enemy.Count; j++)
                {
                    if (enemy[j].getIndex() == index && !(Type == 1 && enemy[j].getLoai() == 1))
                    {
                        enemy[j].setTruMau(Dame, HieuUng, false);
                        if (Type == 1 && isBum == false)
                        {
                            MainPage.PlayHieuUng("TrapEffect", false);
                        }
                        isBum = true;
                        if (Type == 1)
                        {
                            break;
                        }
                    }
                }
            }
        }
    }
}
